package com.android.phoenix.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.phoenix.Constant;
import com.android.phoenix.MainApplication;
import com.android.phoenix.R;

/**
 * @ClassName: StartGuideActivity
 * @Description: 第一次进入引导页面
 * @author: StartGuideActivity
 * @date: 2015-1-31 下午4:27:00
 */
public class StartGuideActivity extends Activity {
	private final static int[] viewId = new int[] {
			R.layout.start_guide_pager1, R.layout.start_guide_pager2,
			R.layout.start_guide_pager3, R.layout.start_guide_pager4 };
	private final static int[] lableId = new int[] { R.id.startGuideLable1,
			R.id.startGuideLable2, R.id.startGuideLable3, R.id.startGuideLable4 };

	private ViewPager vpStartGuide = null;
	private MyViewPagerAdapter mViewPagerAdapter = null;
	private List<View> viewList = null;

	private List<View> lableViews = null;
	private int lastPager = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!isFirstRun()) {
			Intent it = new Intent(StartGuideActivity.this, LoginActivity.class);
			startActivity(it);
			StartGuideActivity.this.finish();
			return;
		}

		setContentView(R.layout.acti_start_guide);
		initViews();
	}

	/**
	 * @Title: isFirstRun
	 * @Description: 判断是否为第一次登录
	 * @author: WanChao
	 * @date: 2015-1-31 下午11:44:50
	 */
	private boolean isFirstRun() {
		return MainApplication.mSharedPreferences.getBoolean(
				Constant.ISFIRSTRUN, true);
	}

	/**
	 * @Title: initViews
	 * @Description: 控件数据初始化
	 * @author: WanChao
	 * @date: 2015-1-31 下午11:44:50
	 */
	private void initViews() {
		vpStartGuide = (ViewPager) findViewById(R.id.vpStartGuide);
		lableViews = new ArrayList<View>();
		for (int i = 0; i < lableId.length; i++) {
			View v = findViewById(lableId[i]);
			lableViews.add(v);
		}

		LayoutInflater inflater = getLayoutInflater();
		viewList = new ArrayList<View>();
		for (int i = 0; i < viewId.length; i++) {
			View v = inflater.inflate(viewId[i], null);
			viewList.add(v);
		}

		mViewPagerAdapter = new MyViewPagerAdapter(this, viewList);
		vpStartGuide.setAdapter(mViewPagerAdapter);
		vpStartGuide.setCurrentItem(0);
		vpStartGuide.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * @ClassName: MyOnPageChangeListener
	 * @Description: viewpager页面变化监听
	 * @author: MyOnPageChangeListener
	 * @date: 2015-2-1 下午9:30:31
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageSelected(int arg0) {
			if (lastPager == arg0) {
				return;
			}
			lableViews.get(lastPager).setBackgroundResource(
					R.drawable.startguide_lable_no);
			lableViews.get(arg0).setBackgroundResource(
					R.drawable.startguide_lable_yes);
			lastPager = arg0;
		}
	}

	/**
	 * @ClassName: MyViewPagerAdapter
	 * @Description: viewpager数据适配器
	 * @author: MyViewPagerAdapter
	 * @date: 2015-2-1 下午9:30:00
	 */
	public class MyViewPagerAdapter extends PagerAdapter {
		private List<View> viewList = null;
		private Context context = null;

		public MyViewPagerAdapter(Context context, List<View> viewList) {
			this.viewList = viewList;
			this.context = context;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position), 0);
			if (position == (viewList.size() - 1)) {
				TextView tvGotoApp = (TextView) viewList.get(position)
						.findViewById(R.id.tvGotoApp);
				tvGotoApp.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Intent it = new Intent(context, LoginActivity.class);
						context.startActivity(it);
						overridePendingTransition(R.anim.slide_right_in,
								R.anim.slide_left_out);
						StartGuideActivity.this.finish();
						MainApplication.mSharedPreferences.edit()
								.putBoolean(Constant.ISFIRSTRUN, false)
								.commit();
					}
				});
			}
			return viewList.get(position);
		}

		@Override
		public int getCount() {
			return viewList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
	}
}
